package com.mukefile.service.impl;

import com.mukefile.dao.UserFileDao;
import com.mukefile.dao.UserFolderDao;
import com.mukefile.dto.DeleteFileDto;
import com.mukefile.dto.FileListDto;
import com.mukefile.pojo.ResponseData;
import com.mukefile.pojo.SysProfile;
import com.mukefile.pojo.UserFile;
import com.mukefile.pojo.UserFolder;
import com.mukefile.service.UserFileService;
import com.mukefile.utils.CommonUtil;
import com.mukefile.utils.GetPageInfo;
import com.mukefile.utils.PageInfo;
import com.mukefile.utils.constant.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserFileServiceImpl implements UserFileService {
    @Resource
    private UserFileDao userFileDao;
    @Resource
    private UserFolderDao userFolderDao;

    @Override
    public int addFolder(UserFolder folder) {
        return userFileDao.insertFolder(folder);
    }

    @Override
    public void renameFolder(UserFolder folder) {
        userFileDao.updateFolderName(folder);
    }

    @Override
    public void renameFile(UserFile file) {
        userFileDao.updateFileName(file);
    }

    @Override
    public ResponseData<FileListDto> getAllFileAndFolder(Integer userId,Long pid, Integer page, Integer limit) {

        List<FileListDto> fileListDtoList = new ArrayList<>();
        // 创建PageInfo对象
        PageInfo<FileListDto> pageInfo = new PageInfo<>();
        // 获取limit查询参数
        GetPageInfo<FileListDto> pi  = new GetPageInfo<>();
        pi.limitPage(page-1,limit);
        List<Object[]> list = userFileDao.selectAllFolderAndFile(userId,pid, pi.getLimitStart(), pi.getLimitEnd());
        // 组装传输对象
        FileListDto dto = null;
        for (Object[] obj:list){
            dto = new FileListDto();
            dto.setId(((BigInteger) obj[0]).longValue());
            dto.setName((String) obj[1]);
            dto.setPid(((BigInteger) obj[2]).longValue());
            dto.setUpdateTime(CommonUtil.timestamp2Format((Timestamp) obj[3]));
            dto.setType((String) obj[4]);
            Long o = Objects.isNull(obj[5])?null:((BigInteger) obj[5]).longValue();
            dto.setSize(CommonUtil.byte2Size(o));
            fileListDtoList.add(dto);
        }
        long count = userFileDao.selectFolderAndFileCount(userId,pid);
        return new ResponseData<>((int) count, fileListDtoList);
    }

    @Override
    public UserFolder getFolderById(Long id) {
        return this.userFileDao.selectFolderById(id);
    }

    @Override
    public void deleteFiles(List<DeleteFileDto> deleteFiles) {
        List<DeleteFileDto> files = new ArrayList<>();
        List<DeleteFileDto> folders = new ArrayList<>();
        for (DeleteFileDto dto:deleteFiles){
            if (Objects.equals(Constant.FILE,dto.getType())){
                files.add(dto);
            } else {
                folders.add(dto);
            }
        }
        userFileDao.batchUpdateFiles(files,"Y");
        userFolderDao.batchUpdateFolder(folders,"Y");
    }
}
